<?php
/**
* 我的银行卡
*/
class Card extends CS_Controller
{
	public function _init()
	{
		$this->load->helper(array('validation'));
		$this->load->library('sms/sms');
		$this->load->model('user_model', 'user');
		$this->load->model('user_bank_model', 'user_bank');
		$this->load->model('pay_method_model', 'pay_method');
		$this->load->model('getpwd_phone_model', 'getpwd_phone');
	}

	/**
	 * 银行卡
	 */
	public function bankCard()
	{
		$this->data['userInfo'] = $this->user->findById($this->uid)->row();
		$this->data['bankList'] = $this->user_bank->findByUid($this->uid);
		$this->load->view('home/card/bankCard', $this->data);
	}

	/**
	 * 绑定银行卡
	 */
	public function bindBankCard()
	{
		$this->data['bankList'] = $this->pay_method->findByShortcut(2);
		$this->load->view('home/card/bindBankCard', $this->data);
	}

	/**
	 * 绑定银行卡
	 */
	public function bindBankCardPost()
	{
		$postData = $this->input->post();
		$postData['bank_name'] = $this->_validate($postData);
		$postData['uid'] = $this->uid;
		$result = $this->user_bank->insertUserBank($postData);
		if ($result) {
			$this->jsonMessage('', '银行卡绑定成功');
		} else {
			$this->jsonMessage('银行卡绑定失败');
		}
	}

	/**
	 * 修改银行卡
	 */
	public function modifyBankCard($id)
	{
		$result = $this->user_bank->findById($id);
		if ( $result->num_rows() == 0 ) {
			$this->error('home/card/bankCard', '', '网络繁忙，请稍后重试');
		}
		$this->data['userBank'] = $result->row();
		$this->load->view('home/card/modifyBankCard', $this->data);
	}

	/**
	 * 删除银行卡
	 */
	public function deleteBankCard($id)
	{
		$result = $this->user_bank->findById($id);
		if ( $result->num_rows() == 0 ) {
			$this->error('home/card/bankCard', '', '网络繁忙，请稍后重试');
		}

		$result = $this->user_bank->deleteUserBank($id, $this->uid);
		if ( $result ) {
			$this->success('home/card/bankCard', '', '银行卡解绑成功');
		} else {
			$this->error('home/card/bankCard', '', '银行卡解绑成功');
		}
	}
	
	/**
	 * 验证
	 */
	private function _validate($postData)
	{
		if ( ! $postData['bank_id'] )
		{
			$this->jsonMessage('请选择银行卡类型');
		}

		$payBank = $this->pay_method->findById($postData['bank_id']);
		if ($payBank->num_rows() == 0)
		{
			$this->jsonMessage('银行卡类型不正确');
		}

		if ( ! validateBankCard($postData['bank_card']) )
		{
			$this->jsonMessage('请填写正确的银行卡号');
		}

		if ( ! validateChineseName($postData['cardholder']) )
		{
			$this->jsonMessage('请填写正确的持卡人姓名');
		}

		if ( ! validateSfz($postData['pin']) )
		{
			$this->jsonMessage('请输入正确的身份证号码');
		}

		if ( ! validateMobilePhone($postData['mobile_phone']) )
		{
			$this->jsonMessage('请输入正确的手机号码');
		}

		if ( ! strlen($postData['captcha']) )
		{
			$this->jsonMessage('请填写效验码');
		}

		if ( strlen($postData['captcha']) != 6 )
		{
			$this->jsonMessage('短信效验码错误');
		}

		$result = $this->getpwd_phone->validateCaptcha($postData['mobile_phone'], $postData['captcha']);
		if ( $result->num_rows() == 0 )
		{
			$this->jsonMessage('短信效验码错误');
		}

		return $payBank->row()->bank_name;
	}

    /**
     * 发送短信
     */
    public function ajaxSendSms()
    {
        $phone = $this->input->post('mobilePhone');
        if (!validateMobilePhone($phone)) {
            $this->jsonMessage('手机号码格式有误');
        }

        $code = mt_rand(100000, 999999);
        $result = $this->getpwd_phone->findByPhone($phone);
        if ( $result->num_rows() > 0 )
        {
            $status = $this->getpwd_phone->updateGetpwdPhone(array('mobile_phone'=>$phone, 'code'=>$code));
        }
        else
        {
            $status = $this->getpwd_phone->insertGetpwdPhone(array('mobile_phone'=>$phone, 'code'=>$code));
        }
        $this->sendToSms($phone, '你正在绑定银行卡，效验码:'.$code.'，有效期为10分钟。千万不要告诉他人！');
        if ($status)
        {
            $this->jsonMessage('');
        }
        else
        {
            $this->jsonMessage('网络繁忙，请稍后重新获取验证码');
        }
	}
}